package iamutkarshtiwari.github.io.ananas.editimage.widget;

import iamutkarshtiwari.github.io.ananas.ResourceTable;
import iamutkarshtiwari.github.io.ananas.editimage.EditImageAbility;
import ohos.agp.components.Component;
import ohos.media.image.PixelMap;

/**
 * Created by panyi on 2017/11/15.
 * <p>
 * 前一步 后一步操作类
 */
public class RedoUndoController implements Component.ClickedListener {
    private Component mRootView;
    private Component mUndoBtn;//撤销按钮
    private Component mRedoBtn;//重做按钮
    private EditImageAbility editImageAbility;
    private EditCache mEditCache = new EditCache();//保存前一次操作内容 用于撤销操作

    private EditCache.ListModify mObserver = cache -> updateBtns();

    public RedoUndoController(EditImageAbility ability, Component panelView) {
        this.editImageAbility = ability;
        this.mRootView = panelView;

        mUndoBtn = mRootView.findComponentById(ResourceTable.Id_undo_btn);
        mRedoBtn = mRootView.findComponentById(ResourceTable.Id_redo_btn);

        mUndoBtn.setTouchEventListener((component, touchEvent) -> {
            if (editImageAbility.mode == EditImageAbility.MODE_PAINT) {
                return true;
            } else {
                return false;
            }
        });
        mRedoBtn.setTouchEventListener((component, touchEvent) -> {
            if (editImageAbility.mode == EditImageAbility.MODE_PAINT) {
                return true;
            } else {
                return false;
            }
        });
        mUndoBtn.setClickedListener(this);
        mRedoBtn.setClickedListener(this);

        updateBtns();
        mEditCache.addObserver(mObserver);
    }

    public void switchMainBit(PixelMap mainBitmap, PixelMap newBit) {
        if (mainBitmap == null || mainBitmap.isReleased())
            return;

        mEditCache.push(mainBitmap);
        mEditCache.push(newBit);
    }


    @Override
    public void onClick(Component v) {
        if (v == mUndoBtn) {  //撤销按钮
            undoClick();
        } else if (v == mRedoBtn) {
            redoClick();
        }//end if
    }


    /**
     * 撤销操作
     */
    protected void undoClick() {
        PixelMap lastBitmap = mEditCache.getNextCurrentBit();
        if (lastBitmap != null && !lastBitmap.isReleased()) {
            editImageAbility.changeMainBitmap(lastBitmap, false);
        }
    }

    /**
     * 取消撤销
     */
    protected void redoClick() {
        PixelMap preBitmap = mEditCache.getPreCurrentBit();
        if (preBitmap != null && !preBitmap.isReleased()) {
            editImageAbility.changeMainBitmap(preBitmap, false);
        }
    }

    /**
     * 根据状态更新按钮显示
     */
    public void updateBtns() {
        mUndoBtn.setVisibility(mEditCache.checkNextBitExist() ? Component.VISIBLE : Component.INVISIBLE);
        mRedoBtn.setVisibility(mEditCache.checkPreBitExist() ? Component.VISIBLE : Component.INVISIBLE);
    }

    public void onDestroy() {
        if (mEditCache != null) {
            mEditCache.removeObserver(mObserver);
            mEditCache.removeAll();
        }
    }

}
